const { Model, DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    class RolePermission extends Model {
        static associate(models) {
            // 定义关联关系
            RolePermission.belongsTo(models.Role, {
                foreignKey: 'role_id',
                as: 'role'
            });
            RolePermission.belongsTo(models.Permission, {
                foreignKey: 'permission_id',
                as: 'permission'
            });
        }
    }

    RolePermission.init(
        {
            id: {
                type: DataTypes.BIGINT,
                autoIncrement: true,
                primaryKey: true,
                comment: 'ID'
            },
            role_id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                comment: '角色ID'
            },
            permission_id: {
                type: DataTypes.BIGINT,
                allowNull: false,
                comment: '权限ID'
            }
        },
        {
            sequelize,
            modelName: 'RolePermission',
            tableName: 'role_permissions',
            timestamps: true,
            paranoid: true,
            underscored: true
        }
    );

    return RolePermission;
}; 